# Definition: at3_xrootdnfs
#
#  Xrootd is a high performance network storage system widely used in high energy 
#  physics experiments such as ATLAS and ALICE. The underlying Xroot data transfer 
#  protocol provides highly efficient access to ROOT based data files. This class provides 
#  the basis for creating a simple Xrootd storage system consisting of one 
#  redirector node and one or more data server nodes.
#  
#  This is the configuration for the ATLAS Tier 3 the standalone xrootd server
#  used for the nfs node 
#
# Parameters:
#   - $fqdnRedirector : fully qualified domain name for the redirector
#   - $inventory: inventory directory for the Simple Service Inventory. 
#      Default value is'/atlas/inventory'    
#   - $ossLocalRoot: Local root for the data server inventories. Default is'/local/xrootd/a'
#   - $storagePath: a directory containing internal Xrootd references to files in the system.
#      Default is '/atlas'
#   
# Actions:
#   - installs xrootd-server
#   - sets up the storage directorys, prepares configuration files for xrootd and the
#     the simple storage inventory
#   - starts the xrootd services
#
# Requires:
#   
define at3_xrootdnfs ($fqdnRedirector,$inventory='/atlas/inventory',$ossLocalRoot='/local/xrootd/a',$storagePath='/atlas') 
{
  $version='5'
  $basearch='x86_64'
  yumrepo { 'xrootd-stable':
    descr    => 'XRootD Stable repository',
    baseurl  => "http://xrootd.org/binaries/stable/slc/${version}/${basearch} http://xrootd.cern.ch/sw/repos/stable/slc/${version}/${basearch}",
    gpgcheck => '0',
    enabled  => '1',
    priority => '1',
  }

  file { "/etc/xrootd/xrootd-standalone.cfg":
    ensure 	=> present,
    content	=> template("at3_xrootdnfs/xrootd-standalone.cfg.tpl"),
    require	=> Exec['setup-xrootd-server'],
  } 

  exec { 'install-xrootd-server':
    command => 'yum -y --disablerepo=epel install xrootd-server',
    path => ['/bin','/usr/bin'],
    creates => '/usr/bin/xrootd', 
    require => Yumrepo['xrootd-stable'],
    logoutput => true,
  }


  $ossLocalRootArray= split($ossLocalRoot, '/')
  $ossLocalRootTop= $ossLocalRootArray[1]

  exec { "${ossLocalRoot}${storagePath}":
    command	=> "mkdir -p ${ossLocalRoot}${storagePath}",
    path => ['/bin','/usr/bin'],
    creates => "${ossLocalRoot}${storagePath}",
    require => Exec['setup-xrootd-server'],
    logoutput=>true, 
  }
 
  exec { "${ossLocalRootTop}":
    command => "chown -R xrootd:xrootd /$ossLocalRootTop",
    path => ['/bin','/usr/bin'],
    require => Exec["${ossLocalRoot}${storagePath}"],
    logoutput=>true, 
  }

  exec { 'setup-xrootd-server':
    command 	=> 'service xrootd setup',
    path 	=> ['/sbin','/usr/bin'],
    creates     => ["/etc/xrootd/xrootd-standalone.cfg"],
    require 	=> Exec['install-xrootd-server'],
    logoutput	=> true,
  }

  file { '/etc/sysconfig/xrootd':
    ensure 	=> present,
    content	=> template('at3_xrootdnfs/xrootd.tpl'),
    owner	=> 'xrootd',
    group	=> 'xrootd',
    require => Exec['setup-xrootd-server'],
  } 

  file { '/etc/xrootd/auth_file':
    ensure => present,
    owner  => 'xrootd',
    group  => 'xrootd',
    content => template('at3_xrootdnfs/auth_file.tpl'),
    require => Exec['setup-xrootd-server'],
  }
  #  a directory containing internal Xrootd references to files in the system
  exec { "$storagePath":
    command	=> "mkdir -p $storagePath",
    path	=> ['/bin','/usr/bin'],
    creates	=> "$storagePath",
    require => Exec['setup-xrootd-server'],
  }
    
  # The Simple Server Inventory (SSI) provide means to have an inventory for 
  # each data server (See details in xrootd.org/doc/dev/cms_config.pdf). In 
  # order to configure it you will need to run a second instance of xrootd 
  # daemon as well XrdCnsd process that should run on every data server. We 
  # will configure xrootd cluster that consists of two nodes. Host A is a 
  # redirector node that is running the following daemons
  exec { "$inventory":
    command	=> "mkdir -p $inventory",
    path	=> ['/bin','/usr/bin'],
    creates	=> "$inventory",
    require => Exec['setup-xrootd-server'],
  }

  # Sets the ownership of the $inventory and $storagePath
  # to the xrootd user
  exec { "chown -R xrootd:xrootd $inventory":
    path    => ['/bin','/usr/bin'],
    logoutput => true,
    onlyif => ["test -z '`ls -l $inventory | grep xrootd`'"],
    require => [Exec['setup-xrootd-server'],Exec["$inventory"]],
  }
  exec { "chown -R xrootd:xrootd $storagePath":
    path    => ['/bin','/usr/bin'],
    logoutput => true,
    onlyif => ["test -z '`ls -l $storagePath | grep xrootd`'"],
    require => [Exec['setup-xrootd-server'],Exec["$storagePath"]],
  }
    
  service { 'xrootd':
    name   => 'xrootd',
    ensure => 'running',
    enable => true,
    pattern => '/usr/bin/xrootd',
    require => [Exec["${ossLocalRootTop}"],File['/etc/xrootd/auth_file'],File["/etc/xrootd/xrootd-standalone.cfg"],File['/etc/sysconfig/xrootd'],Exec["$storagePath"],Exec["$inventory"]],
   }

}
